from datetime import datetime
from flask import Flask, render_template, request

app = Flask(__name__)


@app.route("/index")
def index():
    # 从数据库获取数据
    # text = "中国<h1 style='color:red;' onclick='alert(123)'>北京</h1>联通分公司<a href='#'>青海公司</a>"

    text = "中国北京联通分公司"
    date_string = datetime.now().strftime("%Y-%m-%d %H:%M:%S")

    return render_template("index.html", n1=text, x2=date_string)

    return text


@app.route("/detail")
def detail():
    return render_template("detail.html")


@app.route("/reg", methods=["GET"])
def do_reg():
    # 1.接收用户提交的数据
    username = request.args.get('user')
    password = request.args.get('pwd')
    print(username, password)

    # 2.注册：将用户名和密码 写入文件/保存数据库
    with open('account.txt', mode='a', encoding='utf-8') as f:
        line = "{}|{}\n".format(username, password)
        f.write(line)

    # 3.返回信息
    return "注册成功"


@app.route("/reg2", methods=["POST"])
def do_reg2():
    # 1.接收用户提交的数据
    username = request.form.get('user')
    password = request.form.get('pwd')
    print(username, password)

    file_object = request.files.get("fffff")
    print(file_object.filename)
    file_object.save("1.txt")

    # 2.注册：将用户名和密码 写入文件/保存数据库
    with open('account2.txt', mode='a', encoding='utf-8') as f:
        line = "{}|{}\n".format(username, password)
        f.write(line)

    # 3.返回信息
    return "注册成功"


if __name__ == '__main__':
    app.run()
